(function(){
	var obj={};
	obj.ajax=function(type,url,data,headers,context,callback){
		$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
		.done(callback);
	};
	window.j=obj;
})();

function showFoodComponent(template){
	this.model=null;
	this.template=template;
	this.findFoods=function(){
		j.ajax("GET","findFoods",null,null,this,function(customers){
			this.model=customers;
			_render(this.model);
		});
		
	};
	this.findFoodsByIcnumAndType=function(type,icnum){
		j.ajax("GET","findFoodsByIcnumAndType?type="+type+"&icnum="+icnum,null,null,this,function(foodinfo){
			this.model=foodinfo;
			_render(this.model);
		});
		
	};
	this.findFoodsByInputText=function(text,icnum){
		j.ajax("GET","findFoodsByInputText?text="+text+"&icnum="+icnum,null,null,this,function(foods){
			this.model=foods;
			_render(this.model);
		});
	}
	this.findAllFoods=function(){
		j.ajax("GET","findAllFoods",null,null,this,function(foods){
			this.model=foods;
			_render(this.model);
		});
		
	};
	function _render(data){
		template.empty();
		data.forEach(function(item){
			$("#foodTemplate").clone(true)
			.css("display","block")
			.find(".imageTemplate").attr("src",item.fimg).end()
			.find(".fnameTemplate").text(item.fname).end()
			.find(".fpriceTemplate").text(item.fprice).end()
			.find(".orderTemplate").attr("fid",item.fid).end()
			.appendTo(template);
		});
	}
	
}

$(function(){
	var c=new showFoodComponent($("#foodDiv"));
	$("#allFoods").on("click",function(){
		
	});
	$(".sub-menu li").on("click",function(){
		var type = $(this).attr("type");
		var icnum = $(this).attr("icnum");
		c.findFoodsByIcnumAndType(type,icnum);
	});
	$("#findText").bind('input propertychange',function(){
		var text = $(this).val();
		var icnum = $("#merchantFlag").attr("icnum");
		c.findFoodsByInputText(text,icnum);
	});
  
});

